<template>
	<div class="content">
		<div class="clear">
			<Form :model="query" label-position="left" :label-width="100">
				<Input v-model="query.tradeId" class="w200 m-r10" placeholder="发布编号" />
				<Input v-model="query.userId" class="w200 m-r10" placeholder="用户ID" />
				<Select placeholder="请选状态" class="w200 m-r10" v-model="query.state">
					<Option value="-1">请选择状态</Option>
					<Option value="1">进行中</Option>
					<Option value="2">已结束</Option>
					<Option value="3">已领取</Option>
					<Option value="4">异常</Option>
				</Select>
				<DatePicker :value="dataValue" type="daterange" split-panels placeholder="请选择时间段" class="w200 m-r10" format="yyyy-MM-dd" @on-change="getTime">
				</DatePicker>
				<Button type="primary" v-on:click="getQuery">查询</Button>
			</Form>
		</div>
		<Button class="daochu" type="primary" size="large" @click="exportData">
            <Icon type="ios-download-outline"></Icon>导出
		</Button>
		<Spin v-if="dcShow" class="demo-spin-container"></Spin>
		<Table stripe :columns="columns" :data="conditionQuery" ref="table" :loading="loading"></Table>
		<Page :total="info" show-elevator :page-size="pageSize" show-total @on-change="cuctGetQuery" :current.sync="page"></Page>
		<Modal v-model="Modal" title="指定中奖" width='40%' :mask-closable="false">
			<Form :label-width="100" :model="Form" :rules="rules" ref="refuse">
				<FormItem label="幸运号码" prop='luckyNumber'>
					<Input class="w-50" v-model="Form.luckyNumber" placeholder="请输入幸运号码" disabled />
				</FormItem>
				<FormItem label="发布编号" prop='number'>
					<Input class="w-50" v-model="Form.number" placeholder="请关闭改页面重新点击" disabled />
				</FormItem>
				<FormItem label="用户ID" prop='uuid'>
					<Input class="w-50" v-model="Form.uuid" placeholder="请输入幸运号码" disabled />
				</FormItem>
				<FormItem label="" prop='hint'>
					<span class="red m-l10">*请认真检中奖的幸运号码和用户ID是否正确</span>
				</FormItem>
			</Form>
			<div slot="footer">
				<Button type="text" size="large" @click="winCannel">取消</Button>
				<Button type="primary" size="large" @click="win">确定</Button>
			</div>
		</Modal>
	</div>
</template>
<script>
	export default {
		data() {
			return {
				dcShow: false,
				loading: true,
				jsonData: [],
				walletInfo: null, // 总钱包信息
				moneySummary: '', // 提币汇总
				dataValue: ['', ''], // 时间段值
				query: {
					tradeId: '',
					userId: '',
					state: '-1'
				},
				Form: {
					luckyNumber: '',
					number: '',
					uuid: ''
				},
				rules: {
					luckyNumber: [{
						required: true,
						message: '幸运号码不能为空',
						trigger: 'blur'
					}]
				},
				conditionQuery: [], // 条件查询的结果
				info: null, // 总条数
				page: 1, // 页数
				pageSize: 20, // 每页多少条
				current: 1,
				Modal: false,
				winChangeTime: '',
				modelIndex: '', // 审核成功时候需要用到的 ID
				value: '', // 审核失败的时候填入的值
				downList: [],
				detailList: [],
				columns: [{
						title: '发布编号',
						key: 'number',
						align: 'center',
						render: (h, params) => {
							return h('div', params.row.number == null ? '#' : params.row.number)
						}
					},
					{
						title: '用户ID',
						key: 'uuid',
						align: 'center',
						render: (h, params) => {
							return h('div', params.row.uuid == null ? '#' : params.row.uuid)
						}
					},
					{
						title: '幸运号码',
						key: 'luckyNumber',
						align: 'center',
						render: (h, params) => {
							return h('div', params.row.luckyNumber == null ? '#' : params.row.luckyNumber)
						}
					},
					{
						title: '状态',
						key: 'status',
						align: 'center',
						render: (h, params) => {
							return h('div', params.row.status == null ? '#' : params.row.status == '1' ? '进行中' : params.row.status == '2' ? '已结束' : params.row.status == '3' ? '已领取' : params.row.status == '4' ? '异常' : '#')
						}
					},
					{
						title: '记录时间',
						key: 'createTime',
						align: 'center',
						sortable: true,
						render: (h, params) => {
							return h('div', params.row.createTime == null ? '#' : this.stringToTime(params.row.createTime))
						}
					},
					{
						title: '操作',
						key: 'action',
						width: 200,
						align: 'center',
						render: (h, params) => {
							return h('div', [
								h(
									'Button', {
										props: {
											type: 'primary',
											size: 'small',
											disabled: params.row.status != '1'
										},
										style: {
											marginRight: '5px'
										},
										on: {
											click: () => {
												this.Modal = true
												this.Form = {
													number: params.row.number,
													uuid: params.row.uuid,
													luckyNumber: params.row.luckyNumber
												}
											}
										}
									},
									'指定中奖'
								)
							])
						}
					}
				]
			}
		},
		mounted() {
			this.$Message.config({ // 配置弹窗的位置
				top: 250
			})
			this.getQuery() // 提现审核列表查询
		},
		methods: {
			getQuery() { // 提现审核列表查询
				this.loading = true
				this.page = 1
				let params = this.qs.stringify({
					number: this.query.tradeId,
					uuid: this.query.userId,
					startTime: this.dataValue[0] == '' ? '' : this.dataValue[0] + ' 00:00:01', // 起始日期
					endTime: this.dataValue[1] == '' ? '' : this.dataValue[1] + ' 23:59:59', // 结束日期
					current: this.page,
					size: this.pageSize,
					status: this.query.state == '-1' ? '' : this.query.state
				})
				this.axios.post('/publish/join/list', params).then(res => {
					if(res.data.code == 200) {
						this.conditionQuery = res.data.data.records
						this.info = res.data.data.total
						this.loading = false
					} else if(res.data.code == 501) {
						this.$Message.info(res.data.msg)
						localStorage.removeItem('mytoken')
						this.$router.push({
							name: 'login'
						})
					}
				})
			},
			cuctGetQuery(page) { // 提现审核列表分页查询
				this.loading = true
				let params = this.qs.stringify({
					pn: page, // 当前页【可选，默认1】
					size: this.pageSize, // 每页记录数【可选，默认20】
					uuid: this.query.id, // 用户 uuid
					userName: this.query.userName,
					startTime: this.dataValue[0] == '' ? '' : this.dataValue[0] + ' 00:00:01', // 起始日期
					endTime: this.dataValue[1] == '' ? '' : this.dataValue[1] + ' 23:59:59' // 结束日期
				})
				this.axios.post('/admin/financial/transfer/given', params).then(res => {
					if(res.data.code == 200) {
						this.conditionQuery = res.data.data.records
						this.conditionQuery.forEach((row, index) => {
							this.conditionQuery[index].createTime = '\t' + this.conditionQuery[index].createTime
							this.conditionQuery[index].coinId = this.conditionQuery[index].coinId == 0 ? 'EXUU' : '#'
						})
						this.info = res.data.data.total
						this.loading = false
					} else if(res.data.code == 501) {
						this.$Message.info(res.data.msg)
						localStorage.removeItem('mytoken')
						this.$router.push({
							name: 'login'
						})
					}
				})
			},
			getConditionQuery() { // 提现审核列表条件查询
				this.page = 1
				this.loading = true
				let params = this.qs.stringify({
					pn: this.page, // 当前页【可选，默认1】
					size: this.pageSize, // 每页记录数【可选，默认20】
					userName: this.query.userName, // 转账编号
					uuid: this.query.id, // 用户 uuid
					startTime: this.dataValue[0] == '' ? '' : this.dataValue[0] + ' 00:00:01', // 起始日期
					endTime: this.dataValue[1] == '' ? '' : this.dataValue[1] + ' 23:59:59' // 结束日期
				})
				this.axios.post('/admin/financial/transfer/given', params).then(res => {
					if(res.data.code == 200) {
						this.conditionQuery = res.data.data.records
						this.conditionQuery.forEach((row, index) => {
							this.conditionQuery[index].createTime = '\t' + this.conditionQuery[index].createTime
							this.conditionQuery[index].coinId = this.conditionQuery[index].coinId == 0 ? 'EXUU' : '#'
						})
						this.info = res.data.data.total
						this.loading = false
					} else if(res.data.code == 401) {
						this.$Message.info(res.data.msg)
						localStorage.removeItem('mytoken')
						this.$router.push({
							name: 'login'
						})
					}
				})
			},
			winCannel() {
				this.Modal = false
				this.Form = {
					luckyNumber: '',
					number: '',
					uuid: ''
				}
			},
			win() {

			},
			//			winChange($event) {
			//				clearTimeout(this.winChangeTime);
			//				this.winChangeTime = setTimeout(() => {
			//					this.axios.post('/publish/join/search', this.qs.stringify({
			//						luckyNumber: $event.target.value,
			//						number: this.Form.number
			//					})).then((res) => {
			//						if(res.data.code == 200) {
			//							console.log(1);
			//						} else if(res.data.code == 401) {
			//							this.$Message.info(res.data.msg)
			//							localStorage.removeItem('mytoken')
			//							this.$router.push({
			//								name: 'login'
			//							})
			//						} else {
			//							this.$Message.info(res.data.msg)
			//						}
			//					})
			//				}, 500)
			//			},
			exportData() { // 导出
				this.dcShow = true
				this.axios.post('/admin/financial/transfer/given', this.qs.stringify({
					pn: this.page, // 当前页【可选，默认1】
					size: this.pageSize // 每页记录数【可选，默认20】
				})).then(res => {
					if(res.data.code == 200) {
						this.axios.post('/admin/financial/transfer/given', this.qs.stringify({
							pn: 1, // 当前页【可选，默认1】
							size: res.data.data.total // 每页记录数【可选，默认20】
						})).then(res => {
							if(res.data.code == 200) {
								this.downList = res.data.data.records
								this.downList.forEach((row, index) => {
									this.downList[index].createTime = '\t' + this.downList[index].createTime
									this.downList[index].coinId = this.downList[index].coinId == 0 ? 'EXUU' : '#'
									this.jsonData.push({
										uuid: this.downList[index].uuid,
										userName: this.downList[index].userName,
										num: this.downList[index].num,
										coinId: this.downList[index].coinId,
										createTime: this.downList[index].createTime
									})
								})
								let str = `个人ID,登录名,到账数量,币种类型,创建时间\n`
								for(let i = 0; i < this.jsonData.length; i++) {
									for(let item in this.jsonData[i]) {
										str += `${this.jsonData[i][item] + '\t'},`
									}
									str += '\n'
								}
								let uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(str)
								var link = document.createElement('a')
								var blob = new Blob(['\ufeff' + str], {
									type: 'text/csv'
								}) // 解决大文件下载失败
								link.setAttribute('href', URL.createObjectURL(blob))
								link.download = '获赠列表.csv'
								document.body.appendChild(link)
								link.click()
								document.body.removeChild(link)
								this.dcShow = false
								this.$Message.info('导出成功！')
							} else if(res.data.code == 501) {
								this.$Message.info(res.data.msg)
								localStorage.removeItem('mytoken')
								this.$router.push({
									name: 'login'
								})
							}
						})
					} else if(res.data.code == 501) {
						this.$Message.info(res.data.msg)
						localStorage.removeItem('mytoken')
						this.$router.push({
							name: 'login'
						})
					}
				})
			},
			getTime(daterange) { // 处理时间段时间格式
				this.dataValue = daterange
			},
			Fixed(count) {
				count += ''
				let rs = count.indexOf('.')
				if(rs < 0) {
					return count + '.00'
				}
				while(count.length <= rs + 2) {
					count += '0'
				}
				return count.substr(0, rs + 3)
			}
		}
	}
</script>